﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class Week_NoEvaluateWeekPlanForPopUp : System.Web.UI.Page
{
    WeekDateManager dllWeekDateManager = new WeekDateManager();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    Authentication dllAuthentication = new Authentication();
    VerifyCircuitManager dllVerifyCircuitManager = new VerifyCircuitManager();
    WeekPlanofManager dllWeekPlanofManager = new WeekPlanofManager();
    MonthDateManager dllMonthDateManager = new MonthDateManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        _powerTable = dllAuthentication.getPower(objUserInfo.UserName, "待处理周计划");
        for (int i = 0; i < _powerTable.Rows.Count; i++)
        {
            if (_powerTable.Rows[i]["PowerName"].ToString() == "新建")
            {

            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "删除")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "查看")
            {
            }
            if (_powerTable.Rows[i]["PowerName"].ToString() == "修改")
            {
                this.btnOk.Visible = true;
            }
        }
        if (Request.Params["type"] != null && Request.Params["type"].ToString().Length != 0)
        {
            this.drpType.SelectedIndex = this.drpType.Items.IndexOf(this.drpType.Items.FindByValue(Request.Params["type"].ToString()));
        }
        if (!IsPostBack)
        {
            if (this.drpType.SelectedItem.Value == "0")
            {
                //绑定待评估
                bindGridViewCheckNote();
                this.btnOk.Visible = false;
                this.btnVerifyCircuit.Visible = true;
                 
            }
            else
            {
                //绑定是否达成共识
                bindGridView();
                this.btnOk.Visible = true;
                this.btnVerifyCircuit.Visible = true;
            }
        }
    }




    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }



    /// <summary>
    /// 绑定是否达成共识GridView（处理待评估、待审核计划）
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-25</date>
    void bindGridView()
    {
        //根据评估人查询当前用户的待审待评估计划
        object[] _value = new object[1];
        Model.UserInfo objUserInfo = new Model.UserInfo();
        _value[0] = objUserInfo.UserName;
        DataView dv = new DataView(dllWeekPlanofManager.getWeekPlanNoEvaluateAndNoVerifyForPopUp(_value));
        dv.Sort = "WeekPlanSortId";
        this.gdvNoEvaluateWeekPlan.DataSource = dv;
        this.gdvNoEvaluateWeekPlan.DataBind();
        #region 截取字符串
        for (int i = 0; i < this.gdvNoEvaluateWeekPlan.Rows.Count; i++)
        {

            wrapByComma(this.gdvNoEvaluateWeekPlan, i, 6);
            wrapByComma(this.gdvNoEvaluateWeekPlan, i, 7);
            if (this.gdvNoEvaluateWeekPlan.Rows[i].Cells[10].Text == "&nbsp;" || this.gdvNoEvaluateWeekPlan.Rows[i].Cells[10].Text == "&amp;nbsp;")
            {
                this.gdvNoEvaluateWeekPlan.Rows[i].Cells[10].Text = "";
            }

            if (this.gdvNoEvaluateWeekPlan.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[11].FindControl("btnDownLoad");
                _btn.Visible = false;
                Label _lbl = (Label)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[11].FindControl("lblAlert");
                _lbl.Visible = true;
                _lbl.Text = "无下载";
            }
        }
        #endregion
        #region
        for (int i = 0; i < gdvNoEvaluateWeekPlan.Rows.Count; i++)
        {
            //文件下载，判断如果下载了就显示《下载按钮》 反之 就显示“无下载”
            if (gdvNoEvaluateWeekPlan.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)gdvNoEvaluateWeekPlan.Rows[i].Cells[12].FindControl("btnDownLoad");
                _btn.Visible = false;
                Label _lbl = (Label)gdvNoEvaluateWeekPlan.Rows[i].Cells[12].FindControl("lblAlert");
                _lbl.Visible = true;
                _lbl.Text = "无下载";
            }
        }

        #endregion
    }

    /// <summary>
    /// 自动分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvNoEvaluateWeekPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gdvNoEvaluateWeekPlan.PageIndex = e.NewPageIndex;
        bindGridView();
    }
    /// <summary>
    /// 通过按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //根据评估人查询当前用户的待审周计划
        Model.UserInfo objUserInfo = new Model.UserInfo();
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gdvNoEvaluateWeekPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                object[] _value = new object[2];
                _value[0] = Convert.ToInt32(this.gdvNoEvaluateWeekPlan.DataKeys[i]["WeekPlanId"].ToString());
                _value[1] = objUserInfo.UserName;
                _valueList.Add(_value);
                _count++;
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请至少选择一个周计划", Page);
            return;
        }
        //修改发送状态为2，进入审核流程
        if (this.drpType.SelectedItem.Text != null && this.drpType.SelectedItem.Value == "1")
        {
            int result = dllWeekPlanofManager.editWithTransaction(WeekPlanofManager.field.EvaluatePeople.ToString(), _valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("提交周计划评估未成功，请重试！");
            }
            else
            {
                Response.Redirect("NoEvaluateWeekPlan.aspx");
            }
        }
        else
        {
            int result = dllWeekPlanofManager.editWithTransaction1(WeekPlanofManager.field.EvaluatePeople.ToString(), _valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("提交周计划评估意见未成功，请重试！");
            }
            else
            {
                Response.Redirect("NoEvaluateWeekPlan.aspx");
            }
        }

    }
    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvNoEvaluateWeekPlan.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvNoEvaluateWeekPlan.Rows.Count; i++)
            {
                CheckBox ckb = (CheckBox)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[0].FindControl("ckbSelect");
                ckb.Checked = false;
            }
        }
    }
    /// <summary>
    /// 退回按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        object[] _value = new object[2];
        _value[0] = Convert.ToInt32(this.gdvNoEvaluateWeekPlan.DataKeys[i]["WeekPlanId"].ToString());
        _value[1] = ((TextBox)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[11].FindControl("txtCheckNote")).Text;
        //修改周计划发送状态为0，填写checknote字段内容（追加）
        int result = dllWeekPlanofManager.edit(WeekPlanofManager.field.CheckNote.ToString(), _value);

        if (result == 0)
        {
            Utility.Error.RedirectErrorPage("退回周计划未成功，请重试！");
        }
        else
        {
            Response.Redirect("NoEvaluateWeekPlan.aspx");
        }
    }


    /// <summary>
    /// 计划类型选择项改变
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.drpType.SelectedItem.Value == "0")
        {
            //绑定待评估
            bindGridViewCheckNote();
            this.gdvNoEvaluateWeekPlan.Visible = false;
            this.gdvEvaluteCheckNote.Visible = true;
            this.btnOk.Visible = false;
        }
        else
        {
            //绑定是否达成共识            
            bindGridView();
            this.gdvNoEvaluateWeekPlan.Visible = true;
            this.gdvEvaluteCheckNote.Visible = false;
            this.btnOk.Visible = true;
        }
    }
    /// <summary>
    /// 绑定待评估GridView
    /// </summary>
    void bindGridViewCheckNote()
    {
        //根据评估人查询当前用户的待最后一次评估的计划
        Model.UserInfo objUserInfo = new Model.UserInfo();
        object[] _value = new object[1];
        _value[0] = objUserInfo.UserName;
        DataView dv = new DataView(dllWeekPlanofManager.getWeekPlanEvaluatedAndVerifiedForPopUp(_value));
        dv.Sort = "WeekPlanSortId";
        this.gdvEvaluteCheckNote.DataSource = dv;
        this.gdvEvaluteCheckNote.DataBind();
        #region 截取字符串
        for (int i = 0; i < this.gdvEvaluteCheckNote.Rows.Count; i++)
        {
            DropDownList drp = (DropDownList)this.gdvEvaluteCheckNote.Rows[i].Cells[11].FindControl("dropZhuangTai");
            drp.SelectedValue = this.gdvEvaluteCheckNote.DataKeys[i]["PlanSendState"].ToString();
            if (Convert.ToInt32(this.gdvEvaluteCheckNote.DataKeys[i]["PlanSendState"].ToString()) > 3)
            {
                Button btn = (Button)this.gdvEvaluteCheckNote.Rows[i].Cells[12].FindControl("btnTiJiao");
                btn.Enabled = false;
            }

        }
        #endregion
        #region
        for (int i = 0; i < gdvEvaluteCheckNote.Rows.Count; i++)
        {
            //文件下载，判断如果下载了就显示《下载按钮》 反之 就显示“无下载”
            if (gdvEvaluteCheckNote.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)gdvEvaluteCheckNote.Rows[i].Cells[12].FindControl("btnDownLoad1");
                _btn.Visible = false;
                Label _lbl = (Label)gdvEvaluteCheckNote.Rows[i].Cells[12].FindControl("lblAlert1");
                _lbl.Visible = true;
                _lbl.Text = "无下载";
            }
        }

        #endregion
    }

    /// <summary>
    /// 是否达成共识的提交按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnTiJiao_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        DropDownList drp = (DropDownList)this.gdvEvaluteCheckNote.Rows[i].Cells[11].FindControl("dropZhuangTai");
        ListItem _li = drp.SelectedItem;

        if (_li != null)
        {
            if (_li.Text != "请选择")
            {
                object[] _value = new object[3];
                _value[0] = Convert.ToInt32(this.gdvEvaluteCheckNote.DataKeys[i]["WeekPlanId"].ToString());
                TextBox tb = (TextBox)this.gdvEvaluteCheckNote.Rows[i].Cells[11].FindControl("txtCheckNote");
                if (tb.Text.Length < 18)
                {
                    Utility.HttpHelp.alert("评估意见不得少于32个字符", Page);
                    return;
                }
                tb.Text = tb.Text + "\r\n";
                _value[1] = tb.Text;
                _value[2] = Convert.ToInt32(((DropDownList)this.gdvEvaluteCheckNote.Rows[i].Cells[11].FindControl("dropZhuangTai")).SelectedItem.Value);
                //修改年计划发送状态为为传递过来的参数的发送状态，填写checknote字段内容（追加）
                int result = dllWeekPlanofManager.edit(WeekPlanofManager.field.CheckNote.ToString(), WeekPlanofManager.field.PlanSendState.ToString(), _value);
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("提交评估意见未成功，请重试！");
                }
                else
                {
                    bindGridViewCheckNote();
                }
            }
            else
            {
                Utility.HttpHelp.alert("请选择计划完成状态", Page);
            }
        }
    }

    /// <summary>
    /// 查看流程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnVerifyCircuit_Click(object sender, EventArgs e)
    {
        int _count = 0;
        string _url = "";
        int _quarterPlanId = 0;

        for (int i = 0; i < this.gdvEvaluteCheckNote.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvEvaluteCheckNote.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                _quarterPlanId = Convert.ToInt32(this.gdvEvaluteCheckNote.DataKeys[i]["WeekPlanId"].ToString());
            }
        }

        for (int i = 0; i < this.gdvNoEvaluateWeekPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvNoEvaluateWeekPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _count++;
                _quarterPlanId = Convert.ToInt32(this.gdvNoEvaluateWeekPlan.DataKeys[i]["WeekPlanId"].ToString());
            }
        }

        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能选择一个计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个计划", Page);
            return;
        }
        if (_count == 1)
        {
            _url = "../WebCommon/VerifyCircruitDisplay.aspx?PlanId=" + _quarterPlanId + "&PlanType=3";
            Page.RegisterStartupScript("windowOpen",
               "<script language=javascript>window.open('" + _url + "','week','height=150,width=550,top=250,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        }

    }

    /// <summary>
    /// 周改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpWeekDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _li = this.drpType.SelectedItem;
        if (_li != null)
        {
            if (_li.Text == "待审核")
            {
                bindGridView();
            }
            else
            {
                bindGridViewCheckNote();
            }
        }
    }
    /// <summary>
    /// 附件下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvNoEvaluateWeekPlan.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
    }

    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }

    protected void btnDownLoad1_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvEvaluteCheckNote.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
    }
}
